package com.doujiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiang.domain.Movie;
import com.doujiang.domain.dto.HotMovieDTO;
import com.doujiang.domain.dto.MovieDTO;
import com.doujiang.domain.dto.ReviewStatsDTO;
import com.doujiang.mapper.MovieMapper;
import com.doujiang.service.IMovieService;
import com.doujiang.service.IRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 电影Service业务层处理
 * 
 * @author 王伟舰
 * @date 2025-07-19
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService
{
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private IRatingService ratingService;

    @Override
    public List<HotMovieDTO> getHotMovies(Integer limit) {
        return movieMapper.getHotMovies(limit);
    }

    @Override
    public ReviewStatsDTO getReviewStats() {
        return movieMapper.getReviewStats();
    }


    /**
     * 查询电影列表
     * liuyyue
     * @param movieDTO 评分管理
     * @return 评分管理
     */
    @Override
    public List<MovieDTO> selectMovieList(MovieDTO movieDTO)
    {
        List<MovieDTO> movieDTOS;
        // year
        String year = movieDTO.getYear();
        if (year != null && !year.isEmpty()){
            switch (year) {
                case "2025":
                case "2024":
                case "2023":
                case "2022":
                case "2021":
                case "2020":
                    movieDTO.setStartYear(year + "-01-01");
                    movieDTO.setEndYear(year + "-12-31");
                    break;
                case "2010s":
                    movieDTO.setStartYear("2010-01-01");
                    movieDTO.setEndYear("2019-12-31");
                    break;
                case "2000s":
                    movieDTO.setStartYear("2000-01-01");
                    movieDTO.setEndYear("2009-12-31");
                    break;
                case "90s":
                    movieDTO.setStartYear("1990-01-01");
                    movieDTO.setEndYear("1999-12-31");
                    break;
                case "80s":
                    movieDTO.setStartYear("1980-01-01");
                    movieDTO.setEndYear("1989-12-31");
                    break;
                case "更早":
                    movieDTO.setStartYear("1900-01-01");
                    movieDTO.setEndYear("1979-12-31");
                    break;
                default:
                    break;
            }
        }
        // category
        String category = movieDTO.getCategory();

        movieDTOS = switch (category) {
            case "hot" -> baseMapper.selectHotMovieList(movieDTO);
            case "new" -> baseMapper.selectNewMovieList(movieDTO);
            case "high" -> baseMapper.selectHighMovieList(movieDTO);
            case "underrated" -> baseMapper.selectUnderratedMovieList(movieDTO);
            default -> baseMapper.selectMovieList(movieDTO);
        };


        for (MovieDTO movieDTO1 : movieDTOS) {
            movieDTO1.setScore(ratingService.selectRatingByMovieId(movieDTO1.getId()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            movieDTO1.setYear(sdf.format(movieDTO1.getReleaseDate()));
        }
        return movieDTOS;
    }
    @Override
    public Movie getMovieById(Long id) {
        return getById(id);
    }


    //ly
    /**
     * 通过搜索框查询电影
     * 刘跃
     * @param movieDTO 评分管理
     * @return 评分管理
     */
    @Override
    public List<MovieDTO> searchMovieList(MovieDTO movieDTO) {
        List<MovieDTO> movieDTOS = movieMapper.searchMovieList(movieDTO);
        for (MovieDTO movieDTO1 : movieDTOS) {
            movieDTO1.setScore(ratingService.selectRatingByMovieId(movieDTO1.getId()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            movieDTO1.setYear(sdf.format(movieDTO1.getReleaseDate()));
        }
        return movieDTOS;
    }
}
